package cn.zjh.pet.service.impl;

import cn.zjh.basic.service.IBaiduAiService;
import cn.zjh.basic.util.AjaxResult;
import cn.zjh.basic.util.DistanceUtil;
import cn.zjh.basic.util.LoginContext;
import cn.zjh.basic.util.Point;
import cn.zjh.org.domain.Employee;
import cn.zjh.org.domain.Shop;
import cn.zjh.org.mapper.EmployeeMapper;
import cn.zjh.org.mapper.ShopMapper;
import cn.zjh.pet.domain.SearchMasterMsg;
import cn.zjh.pet.domain.SearchMasterMsgAuditLog;
import cn.zjh.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.zjh.pet.mapper.SearchMasterMsgMapper;
import cn.zjh.pet.service.ISearchMasterMsgService;
import cn.zjh.basic.service.impl.BaseServiceImpl;
import cn.zjh.user.domain.Logininfo;
import cn.zjh.user.domain.User;
import cn.zjh.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private IBaiduAiService baiduAiAuditService;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public AjaxResult publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //获取登录人id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        searchMasterMsg.setUserId(user.getId());

        searchMasterMsg.setState(0);//待审核 - 可以不设置
        //2.将寻主消息添加到数据库- 如何审核失败 - 修改寻主消息重新审核【shop_id】
        searchMasterMsgMapper.save(searchMasterMsg); //自增ID

        //3.百度AI智能审核
        Boolean boo1 = baiduAiAuditService.baiduAiText(searchMasterMsg.getName() + searchMasterMsg.getTitle());
        Boolean boo2 = baiduAiAuditService.baiduAiImg(searchMasterMsg.getResources());
        //3.1.审核成功
        if(boo1 && boo2){//成功
            //a.修改状态：0 - 1
            searchMasterMsg.setState(1);//审核通过待处理
            //b1.找到最近的店铺 - 分配店铺【设置到shopId】 - 发送短信给店铺管理员，告诉他去接回宠物
            List<Shop> shops = shopMapper.findAll();
            Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());//用户发布寻主消息的地址 - 宠物的地址
            Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
            //b2.找到店铺 - 绑定店铺 。没有周到就放到寻主池[state=1,shopId=null]
            if(nearestShop!=null){
                searchMasterMsg.setShopId(nearestShop.getId());
            }
            //b3.更新数据库
            searchMasterMsgMapper.update(searchMasterMsg);

            //c.添加审核成功日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(searchMasterMsg.getId());
            log.setState(1);//审核成功
            //百度智能审核
            log.setNote("审核成功");
            searchMasterMsgAuditLogMapper.save(log);

            if(nearestShop!=null){
                Employee employee = employeeMapper.loadByShopId(nearestShop.getId());
                //SmsUtils.sendSms(employee.getPhone(),"你有新的订单，请去"+msg.getAddress()+"接回宠物!!!");
                System.out.println("你有新的订单，请去"+searchMasterMsg.getAddress()+"接回宠物!!!");
            }
            return new AjaxResult();
        }else{
            //3.2.审核失败
            //a.添加审核失败日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(searchMasterMsg.getId());
            log.setState(0);//审核失败
            //百度智能审核
            log.setNote("审核失败");
            searchMasterMsgAuditLogMapper.save(log);
            //b.发送短信给用户告诉他审核失败，修改寻主消息
//            SmsUtils.sendSms(user.getPhone(),"你发布的寻主消息未通过审核，请修改之后重新发布!!!");
            System.out.println("你发布的寻主消息未通过审核，请修改之后重新发布!!!");
            return AjaxResult.me().setSuccess(false).setMsg("审核失败");
        }

    }
}
